package com.zecan.blog.controller;

import com.zecan.blog.common.BlogResult;
import com.zecan.blog.common.status.CommentStatus;
import com.zecan.blog.dto.comment.ReplyCommentDto;
import com.zecan.blog.service.CommentService;
import com.zecan.blog.utils.BlogResultUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName CommentController
 * @Description
 * @date 2022/11/11 11:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    /**
     * 根据文章id 获取回复信息
     * @param articleId
     * @return
     */
    @RequestMapping(value = "/list/{articleId}",method = RequestMethod.GET)
    public BlogResult getArticleComment(@PathVariable("articleId") Integer articleId) {
        return BlogResultUtils.ok(commentService.getArticleCommentList(articleId),"获取文章评论信息成功");
    }


    /**
     * 发送回复
     * @param replyCommentDto
     * @return
     */
    @PreAuthorize("hasAuthority('user') OR hasAuthority('admin')" )
    @RequestMapping(value = "/sendComment",method = RequestMethod.POST)
    public BlogResult sendCommentOrReply(ReplyCommentDto replyCommentDto){
        commentService.sendComment(replyCommentDto);
        return BlogResultUtils.commentStatus(CommentStatus.COMMENT_SEND_SUCCESS);
    }

    /**
     * 删除回复
     * @param commentId
     * @return
     */
    @PreAuthorize("hasAuthority('user') OR hasAuthority('admin')" )
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public BlogResult delComment(Integer commentId) {
        commentService.delComment(commentId);
        return BlogResultUtils.commentStatus(CommentStatus.COMMENT_DEL_SUCCESS);
    }

    /**
     * 获取评论与回复列表
     * @return
     */
    @PreAuthorize("hasAuthority('admin')" )
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public BlogResult getCommentList(Integer offset ,Integer limit) {
        return BlogResultUtils.ok(commentService.getCommentList(offset,limit),"获取评论回复成功");
    }


}
